package com.hhq.sso.system.controller;

import java.util.List;
import java.util.Map;

import org.springblade.core.tool.support.Kv;
import org.springblade.core.tool.utils.Func;
import org.springframework.web.bind.annotation.*;

import com.hhq.sso.auth.base.pojo.SystemUserInfo;
import com.hhq.sso.auth.base.util.SystemUserContext;
import com.hhq.sso.common.api.CommonResult;
import com.hhq.sso.common.page.Condition;
import com.hhq.sso.system.entity.Menu;
import com.hhq.sso.system.pojo.CheckedTree;
import com.hhq.sso.system.pojo.GrantTree;
import com.hhq.sso.system.service.IMenuService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;

/**
 * 控制器
 *
 * @author Chill
 */
@RestController
@AllArgsConstructor
@RequestMapping("/menu")
@Tag(name = "菜单", description = "菜单")
public class MenuController {

    private IMenuService menuService;

    /**
     * 详情
     */
    @GetMapping("/detail")
    @Operation(summary = "详情", description = "传入menu")
    public CommonResult<Menu> detail(Menu menu) {
        Menu detail = menuService.getOne(Condition.getQueryWrapper(menu));
        return CommonResult.data(detail);
    }

    /**
     * 列表
     */
    @GetMapping("/list")
    @Parameters({
        @Parameter(name = "code", description = "菜单编号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "name", description = "菜单名称", in = ParameterIn.QUERY, schema = @Schema(type = "string"))})
    @Operation(summary = "列表", description = "传入menu")
    public CommonResult<List<Menu>> list(@Parameter(hidden = true) @RequestParam Map<String, Object> menu) {
        List<Menu> list =
            menuService.list(Condition.getQueryWrapper(menu, Menu.class).lambda().orderByAsc(Menu::getSort));
        return CommonResult.data(list);
    }

    /**
     * 菜单列表
     */
    @GetMapping("/menu-list")
    @Parameters({
        @Parameter(name = "code", description = "菜单编号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "name", description = "菜单名称", in = ParameterIn.QUERY, schema = @Schema(type = "string"))})
    @Operation(summary = "菜单列表", description = "传入menu")
    public CommonResult<List<Menu>> menuList(@Parameter(hidden = true) @RequestParam Map<String, Object> menu) {
        List<Menu> list = menuService.list(
            Condition.getQueryWrapper(menu, Menu.class).lambda().eq(Menu::getCategory, 1).orderByAsc(Menu::getSort));
        return CommonResult.data(list);
    }

    /**
     * 懒加载菜单列表
     */
    @GetMapping("/lazy-menu-list")
    @Parameters({
        @Parameter(name = "code", description = "菜单编号", in = ParameterIn.QUERY, schema = @Schema(type = "string")),
        @Parameter(name = "name", description = "菜单名称", in = ParameterIn.QUERY, schema = @Schema(type = "string"))})
    @Operation(summary = "懒加载菜单列表", description = "传入menu")
    public CommonResult<List<Menu>> lazyMenuList(Long parentId,
        @Parameter(hidden = true) @RequestParam Map<String, Object> menu) {
        List<Menu> list = menuService.lazyMenuList(parentId, menu);
        return CommonResult.data(list);
    }

    /**
     * 新增或修改
     */
    @PostMapping("/submit")
    @Operation(summary = "新增或修改", description = "传入menu")
    public CommonResult<Boolean> submit(@Valid @RequestBody Menu menu) {
        return CommonResult.status(menuService.saveOrUpdate(menu));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @Operation(summary = "删除", description = "传入ids")
    public CommonResult<Boolean> remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
        return CommonResult.status(menuService.removeByIds(Func.toLongList(ids)));
    }

    /**
     * 前端菜单数据
     */
    @GetMapping("/routes")
    @Operation(summary = "前端菜单数据", description = "前端菜单数据")
    public CommonResult<List<Menu>> routes() {
        SystemUserInfo systemUserInfo = SystemUserContext.getSystemUserInfo();
        List<Menu> list = menuService
            .routes((systemUserInfo == null || systemUserInfo.getUserId() == 0L) ? null : systemUserInfo.getRoleIds());
        return CommonResult.data(list);
    }

    /**
     * 前端按钮数据
     */
    @GetMapping("/buttons")
    @Operation(summary = "前端按钮数据", description = "前端按钮数据")
    public CommonResult<List<Menu>> buttons() {
        SystemUserInfo systemUserInfo = SystemUserContext.getSystemUserInfo();
        List<Menu> list = menuService.buttons(systemUserInfo.getRoleIds());
        return CommonResult.data(list);
    }

    /**
     * 获取菜单树形结构
     */
    @GetMapping("/tree")
    @Operation(summary = "树形结构", description = "树形结构")
    public CommonResult<List<Menu>> tree() {
        List<Menu> tree = menuService.tree();
        return CommonResult.data(tree);
    }

    /**
     * 获取权限分配树形结构
     */
    @GetMapping("/grant-tree")
    @Operation(summary = "权限分配树形结构", description = "权限分配树形结构")
    public CommonResult<GrantTree> grantTree() {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        GrantTree vo = new GrantTree();
        vo.setMenu(menuService.grantTree(user));
        vo.setDataScope(menuService.grantDataScopeTree(user));
        return CommonResult.data(vo);
    }

    /**
     * 获取权限分配树形结构
     */
    @GetMapping("/role-tree-keys")
    @Operation(summary = "角色所分配的树", description = "角色所分配的树")
    public CommonResult<CheckedTree> roleTreeKeys(String roleIds) {
        CheckedTree vo = new CheckedTree();
        vo.setMenu(menuService.roleTreeKeys(roleIds));
        vo.setDataScope(menuService.dataScopeTreeKeys(roleIds));
        return CommonResult.data(vo);
    }

    /**
     * 获取配置的角色权限
     */
    @GetMapping("auth-routes")
    @Operation(summary = "菜单的角色权限")
    public CommonResult<List<Kv>> authRoutes() {
        SystemUserInfo user = SystemUserContext.getSystemUserInfo();
        if (Func.isEmpty(user) || user.getUserId() == 0L) {
            return null;
        }
        return CommonResult.data(menuService.authRoutes(user));
    }

}
